package com.cloud.sysmana.web;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.cloud.sysmana.dto.RetIndex;
import com.cloud.sysmana.dto.SysLogInfo;
import com.cloud.sysmana.entity.SysLog;
import com.cloud.sysmana.entity.SysUser;
import com.cloud.sysmana.entity.SysUserRole;
import com.cloud.sysmana.entity.dao.ISysLogDao;
import com.cloud.sysmana.entity.dao.ISysUserDao;
import com.cloud.sysmana.entity.dao.ISysUserRoleDao;
import com.cloud.util.base.BaseAction;
import com.cloud.util.base.IUserInfo;
import com.cloud.util.base.RetType;
import com.cloud.util.base.UserInfoCache;
import com.cloud.util.net.NetUtil;
import com.cloud.util.other.MD5;

/**
 * 系统登录
 * @author gaof
 * @date 2016年12月24日
 */
@Controller
@Transactional
@RequestMapping("system")
public class LoginAction extends BaseAction{

	@Resource(name="sysUserDao")
	private ISysUserDao sysUserDao;
	
	@Resource(name="sysLogDao")
	private ISysLogDao sysLogDao;
	
	@Resource(name="sysUserRoleDao")
	private ISysUserRoleDao sysUserRoleDao;
	
	/**
	 * 登录校验
	 * @param modelMap
	 * @param number
	 * @param pwd
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "loginCheck.dop")
	public RetType loginCheck(ModelMap modelMap, String number, String pwd, HttpServletRequest request, HttpServletResponse response){
		
		//通过输入账号获取用户
		SysUser user = sysUserDao.getUserByNo(number);
		if (user == null) {
			RetType retType = new RetType();
			retType.setErrCode("2");
			retType.setErrMsg("用户不存在！");
			
			return retType;
		}
		
		if(!user.getPwd().equals(MD5.toMD5(pwd.getBytes()))){
			RetType retType = new RetType();
			retType.setErrCode("2");
			retType.setErrMsg("密码不正确！");
			
			return retType;
		}
		
		//获取用户权限
		List<SysUserRole> userRoles = sysUserRoleDao.getUserRolesByUserId(user.getUserId());
		/* 缓存用户个人信息 */
		UserInfoCache userInfoCache = getUserInfoCache(request);
		if (null == userInfoCache) {
			userInfoCache = new UserInfoCache();
			request.getSession().setAttribute(UserInfoCache.USER_INFO_SESSION_CACHE_KEY, userInfoCache);
		}
		userInfoCache.setUserInfo(user);
		userInfoCache.setUserRoles(userRoles);
		
		//记录系统日志
		SysLog log = new SysLog("用户登录", new Date(), user.getUserId(), user.getUserName(), SysLogInfo.LOG_MOD_LOGIN, NetUtil.getRemoteHost(request));
		sysLogDao.addLog(log);
		
		RetType retType = new RetType();
		retType.setErrCode("1");
		
		return retType;
	}
	//调用消息提醒service  得到消息
//	@Resource(name="bgMessageReminderService")
//	private IBgMessageReminderService bgMessageReminderService;
	/**
	 * 登录跳转
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "login.dop")
	public String login(HttpServletRequest request,ModelMap modelMap){
		IUserInfo userInfo = getUserInfo(request);
		if (userInfo!=null) {
			if(userInfo.getUserId()!=null){
				List list = sysUserRoleDao.getUserRoleByUserId(userInfo.getUserId());
				Object userRole=list.get(0);
				HttpSession session = request.getSession();
				session.setAttribute("userRole", userRole);
				modelMap.put("userInfo", userInfo);
				
				//调用消息提醒service  得到消息
				//MessageReminderList mReminderList = bgMessageReminderService.getMessageReminderList(userInfo);
				//modelMap.put("mReminderList", mReminderList);
				return getViewsModelPathSys("mainIndex");
				}
			else {
				return getViewsModelPathSys("login");
			}
		}
		
		else{
			return getViewsModelPathSys("login");
		}
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	@RequestMapping(value = "loginOut.dop")
	public String loginOut(HttpServletRequest request){
		
		UserInfoCache userInfoCache = getUserInfoCache(request);
		if (null == userInfoCache) {
			return getViewsModelPathSys("login");
		}
		
		IUserInfo userInfo = userInfoCache.getUserInfo();
		if (null == userInfo) {
			return getViewsModelPathSys("login");
		}
		
		request.getSession().removeAttribute(UserInfoCache.USER_INFO_SESSION_CACHE_KEY);// 个人资源清空
		request.getSession().invalidate();// 释放会话
		
		//记录系统日志
		SysLog log = new SysLog("用户退出登录", new Date(), userInfo.getUserId(), userInfo.getUserName(), SysLogInfo.LOG_MOD_LOGINOUT, NetUtil.getRemoteHost(request));
		sysLogDao.addLog(log);

		return getViewsModelPathSys("login");
	}
	
	/**
	 * 首页登录
	 * @return
	 */
	@RequestMapping(value="index.do")
	public String index(HttpServletRequest request,ModelMap modelMap,RetIndex retIndex){
		IUserInfo userInfo=getUserInfo(request);
		String userName=userInfo.getUserName();
		modelMap.put("userName", userName);
		return getViewsModelPathSys("index");
	}
}
